<?php


use Qcloud\Sms\SmsSingleSender;
use Qiniu\Auth;
use Qiniu\Storage\UploadManager;

function sendMsg($mobile, $code){
    // 短信应用SDK AppID
    $appid = "1400438433"; // 1400开头
    // 短信应用SDK AppKey
    $appkey = "b604d3afd7bd4c6a865b5dbf65396746";
    // 短信模板ID，需要在短信应用中申请
    $templateId = 747068;  // NOTE: 这里的模板ID`7839`只是一个示例，真实的模板ID需要在短信控制台中申请
    // 签名
    $smsSign = "58tbook"; // NOTE: 这里的签名只是示例，请使用真实的已申请的签名，签名参数使用的是`签名内容`，而不是`签名ID`
    // 指定模板ID单发短信
    try {
        $ssender = new SmsSingleSender($appid, $appkey);
        $params = [$code,2];
        $result = $ssender->sendWithParam("86", $mobile, $templateId, $params, $smsSign, "", "");  // 签名参数未提供或者为空时，会使用默认签名发送短信
        $rsp = json_decode($result,true);
        if($rsp['result'] == 0){
            return ['code'=>1,'msg'=>"success"];
        }else{
            return ['code'=>0,'msg'=>$rsp['errmsg']];
        }
    } catch(\Exception $e) {
        return ['code'=>0,'msg'=>$e->getMessage()];
    }
}


function upload(){

    $accessKey = 'fSvRlbizGwxjKVM0CewkZuJIxQkcZfZ0ushbTEp3';
    $secretKey = 'E0POvtUZOoRi4qqXXPbhET5wmSD4UUy8XACg7J-f';
    // 初始化签权对象
    $auth = new Auth($accessKey, $secretKey);

    $bucket = 'ccuke';
    // 生成上传Token
    $token = $auth->uploadToken($bucket);
    // 构建 UploadManager 对象
    $uploadMgr = new UploadManager();

    $name=$_FILES['file']['name'];

    $filePath=$_FILES['file']['tmp_name'];

    $type=$_FILES['file']['type'];

    list($ret,$err)=$uploadMgr->putFile($token,$name,$filePath,null,$type,false);
//    var_dump($ret);
//    var_dump($err);die;

    if($err !== null) {
        return $err;
    } else {
        echo $ret['domain'] . '/' . $ret['key'];
    }

}


function isEmail($email){
    $pattern = "/^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,})$/";
    return preg_match($pattern, $email);
}



